package com.freewater.springboot.demo.spel.helloworld;

import com.freewater.springboot.demo.spel.Inventor;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    public static void main(String[] args) {
        ExpressionParser parser = new SpelExpressionParser();
        // 字面量常量
        Expression expression = parser.parseExpression("'Hello World'");
        System.out.println(((String) expression.getValue()));
        // 调用方法
        expression = parser.parseExpression("'Hello World'.concat('!')");
        System.out.println((String) expression.getValue());
        // invokes 'getBytes()'
        expression = parser.parseExpression("'Hello World'.bytes");
        byte[] bytes = (byte[]) expression.getValue();
        System.out.println(Arrays.toString(bytes));
        // invokes 'getBytes().length'
        expression = parser.parseExpression("'Hello World'.bytes.length");
        int length = (Integer) expression.getValue();
        System.out.println(length);
        expression = parser.parseExpression("new String('hello world').toUpperCase()");
        String message = expression.getValue(String.class);
        System.out.println(message);
        // Create and set a calendar
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(1856, Calendar.AUGUST, 9);
        Inventor tesla = new Inventor("Nikola Tesla", calendar.getTime(), "Serbian");
        expression = parser.parseExpression("name");
        // Use root object
        final String name = (String) expression.getValue(tesla);
        System.out.println(name);
        expression = parser.parseExpression("name == 'Nikola Tesla'");
        Boolean result = (Boolean) expression.getValue(tesla);
        System.out.println(result);
    }
}
